package cn.edu.hizee.gps.controller;

import cn.edu.hizee.gps.dao.UserInfoRepository;
import cn.edu.hizee.gps.dao.UserRepository;
import cn.edu.hizee.gps.model.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;

@Controller
@RequestMapping("userInfo")
public class UserInfoController extends BaseController {

    @Autowired
    private UserInfoRepository userInfoRepository;

    @Autowired
    private UserRepository userRepository;


    /**
     * 添加个人信息
     * @param userId
     * @param realname
     * @param nickname
     * @param sfz
     * @param height
     * @param blood
     * @param sex
     * @return
     */
    @PostMapping("accept_userinfo")
    public String acceptParams(String userId, String realname, String nickname, String sfz, String height, String blood, String sex, HttpServletResponse response){
        Integer id = Integer.valueOf(userId);
        if(userRepository.findAllById(id) != null) {
//            System.out.println(userInfoRepository.findByUserId(id));
            if(userInfoRepository.findByUserId(id) == null) {
                UserInfo userInfo = new UserInfo();
                userInfo.setSfzno(sfz);
                userInfo.setHeight(height);
                userInfo.setSex(sex);
                userInfo.setBlood(blood);
                userInfo.setUserId(id);
                userInfoRepository.save(userInfo);
            }else {
                userInfoRepository.updateByUserId(sfz,height,blood,sex,id);
            }
            userRepository.updateByNickNameAndRealName(realname, nickname, id);
        }
        return ajaxReturn(response,1,"保存成功",null);

    }

    /*@PostMapping("find_user_info")
    public String findUserInfo(String userId, HttpServletResponse response){
        userInfoRepository.
    }*/


}
